/*
 * @Author: fhw
 * @Date: 2022-07-26 10:13:58
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-03-31 10:20:46
 * @Description:
 */
'use strict';

import { HttpException, HttpStatus } from '@nestjs/common';
import Decimal from 'decimal.js';
/** 是否中文 */
export function isChinese(temp) {
  if (/.*[\u4e00-\u9fa5]+.*$/.test(temp)) {
    return true;
  }
  return false;
}
/** 报错 */
export function throwHttp(message: string, status?: 400 | 200 | 401) {
  throw new HttpException(
    {
      status: status ?? HttpStatus.BAD_REQUEST,
      message: message,
    },
    HttpStatus.BAD_REQUEST,
  );
}
export interface StatusCode<T> {
  status: 400 | 200;
  message: string | T;
  success: boolean;
}
/** 错误状态 */
export function errorState(message: string): StatusCode<string> {
  return {
    status: 400,
    message,
    success: false,
  };
}
/** 成功状态 */
export function successState<T>(message: string | T): StatusCode<string | T> {
  return {
    status: 200,
    message,
    success: true,
  };
}
interface Pagination {
  pageSize: number;
  pageNum: number;
}
/** 分页处理 */
export function getPagination({
  pageSize = 10,
  pageNum = 1,
}: Pagination): Pagination {
  const size = Number(pageSize) ?? 10;
  let num = Number(pageNum)
  if (num !== 0) {
    num -= 1;
  }
  return {
    pageSize: size,
    pageNum: num,
  };
}
/** 处理排序 */
export function getOrders(orders: string) {
  let order = {}
  try {
    order = JSON.parse(orders) ?? { createDate: 'DESC' }
  } catch (error) {
    order = { createDate: 'DESC' }
  }
  return order
}

/**
 * 校验密码
 * @param {string} value 密码
 * @param {Function} callback 回调
 * @returns
 */
export function checkPassword(value: string) {
  let level = '';
  // 校验是数字
  const regex1 = /^\d+$/;
  // 校验字母
  const regex2 = /^[A-Za-z]+$/;
  // 校验符号
  const regex3 =
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]+$/;
  if (regex1.test(value)) {
    level = '低';
  } else if (regex2.test(value)) {
    level = '低';
  } else if (regex3.test(value)) {
    level = '低';
  } else if (/^[A-Za-z\d]+$/.test(value)) {
    level = '中';
  } else if (
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、\d]+$/.test(
      value,
    )
  ) {
    level = '中';
  } else if (
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、A-Za-z]+$/.test(
      value,
    )
  ) {
    level = '中';
  } else if (
    /^[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、A-Za-z\d]+$/.test(
      value,
    )
  ) {
    level = '高';
  }
  return level;
}

/**
   * 判断是否在开始截止时间之间
   * @param {string} startDate 
   * @param {string} endDate 
   * @returns {boolean}
   */
const dayjs = require('dayjs')
const isBetween = require('dayjs/plugin/isBetween')
const dayOfYear = require('dayjs/plugin/dayOfYear')
// 注册isBetween插件
dayjs.extend(isBetween)
dayjs.extend(dayOfYear)
export function getStatus(startDate: string, endDate: string): boolean {
  return dayjs(new Date()).isBetween(dayjs(startDate).format('YYYY-MM-DD HH:mm:ss'), dayjs(endDate), 'second', '[]')
}


/**
 * 计算金额是否足够
 * @param userAmount 用户余额
 * @param price 价格
 * @returns 是否足够
 */
export function isAmountSufficient(userAmount: number, price: number): boolean {
  return Number(userAmount) >= new Decimal(price).times(100).toNumber();
}